﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Xpo;
using Liekhus.Testing.Wiki.Module;
using Liekhus.TimeTracker.Module.Web.Providers;

namespace Liekhus.Testing.Wiki.Controllers
{
    public class WikiController : Controller
    {
        //
        // GET: /Wiki/
        public ActionResult Index()
        {
            XPQuery<Page> pages = new XPQuery<Page>(XpoConnectionHelper.GetSession<Page>());
            var collection = from p in pages
                             select p;

            ViewData.Model = collection;

            return View();
        }

        //
        // GET: /Wiki/Details/5
        public ActionResult Details(string id)
        {
            XPQuery<Page> pages = new XPQuery<Page>(XpoConnectionHelper.GetSession<Page>());
            var page = (from p in pages
                        where p.Name == id
                        select p).First<Page>();

            ViewData.Model = page;

            return View();
        }

        //
        // GET: /Wiki/Create
        public ActionResult Create()
        {
            Page page = new Page(XpoConnectionHelper.GetSession<Page>());
            ViewData.Model = page;
            return View();
        } 

        //
        // POST: /Wiki/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Wiki/Edit/5
        public ActionResult Edit(Guid id)
        {
            XPQuery<Page> pages = new XPQuery<Page>(XpoConnectionHelper.GetSession<Page>());
            var page = (from p in pages
                        where p.Oid == id
                        select p).First<Page>();

            ViewData.Model = page;

            return View();
        }

        //
        // POST: /Wiki/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Wiki/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Wiki/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
